package com.twxm.breed.util;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.twxm.breed.util.UploadUtil.getProjectPath;

public class ExcelAnalyzeUtil {
    public static String excelAnalyze(String path, List<HashMap<String,String>> list) {
        try {
            Set<String> oxygen = new HashSet<>(GetTableUtil.getTable("Oxygen"));
            Set<String> water = new HashSet<>(GetTableUtil.getTable("Water"));
            Set<String> weather = new HashSet<>(GetTableUtil.getTable("Weather"));

            FileInputStream inputStream = new FileInputStream(path);
            Workbook workbook = new XSSFWorkbook(inputStream);
            // 获取第一个工作表
            Sheet sheet = workbook.getSheetAt(0);
            Row keyRow = sheet.getRow(0);
            ArrayList<String> keys = new ArrayList<>();
            for (int j = 0; j < keyRow.getLastCellNum(); j++) {
                Cell cellkey = keyRow.getCell(j);
                cellkey.setCellType(CellType.STRING);
                String key = cellkey.getStringCellValue();
                keys.add(key);
            }
            String table = "";
            Set<String> keySet = new HashSet<>(keys);
            if (keySet.containsAll(oxygen)){
                keySet = oxygen;
                table = "DissolvedOxygen";
            } else if (keySet.containsAll(water)) {
                keySet = water;
                table = "WaterFactor";
            } else if (keySet.containsAll(weather)) {
                keySet = weather;
                table = "WeatherFactor";
            }else {
                return table;
            }

            // 遍历每一行数据
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);

                HashMap<String, String> dataMap = new HashMap<>();

                // 遍历每一列数据
                for (int j = 0; j < row.getLastCellNum(); j++) {
                    Cell cell = row.getCell(j);
                    String key = keys.get(j);
                    //只要需要的字段
                    if (!keySet.contains(key)){
                        continue;
                    }
                    GetTableUtil getTableUtil = new GetTableUtil();
                    //判断类型对不对
                    if (!getTableUtil.getType(key).equals(cell.getCellType().toString())){
                        return "";
                    }
                    //判断是不是采集地点，只有采集地点是字符串类型
                    if (keys.get(j).equals(getTableUtil.getName("采集地点"))){
                        dataMap.put(key, cell.getStringCellValue());
                        continue;
                    }
                    //判断采集时间是否为时间类型
                    if (!keys.get(j).equals(getTableUtil.getName("采集时间"))){
                        DataFormatter formatter = new DataFormatter();
                        String cellValue = formatter.formatCellValue(cell);
                        dataMap.put(key, String.valueOf(cellValue));
                        continue;
                    }
                    if (!DateUtil.isCellDateFormatted(cell)) {
                        return "";
                    }
                    Date date = DateUtil.getJavaDate(cell.getNumericCellValue());
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
                    dataMap.put(key,sdf.format(date));

                }
//                //剔除多余的字段信息
//                Set<String> allKeys = dataMap.keySet();
//                if (table.equals("DissolvedOxygen")){
//                    allKeys.retainAll(oxygen);
//                } else if (table.equals("WaterFactor")) {
//                    allKeys.retainAll(water);
//                }else{
//                    allKeys.retainAll(weather);
//                }
                list.add(dataMap);
            }
            return table;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

}
